/**
 * knex config
 */
'use strict';

// env
require('../env');

const Url = require('url');
const lodash = require('lodash');

// knex connection 字符串配置不支持 charset
// 手动解析一次
const connectionParsed = Url.parse(process.env.APP_DB, true);
const connection = lodash.assign(connectionParsed.query, {
  host: connectionParsed.hostname,
  port: connectionParsed.port || 3306,
  database: connectionParsed.pathname.replace('/', ''),
});

// auth
const auth = connectionParsed.auth;
if (auth) {
  const arr = auth.split(':');

  connection.user = arr[0];
  connection.password = arr[1] || undefined;
}

// config
const config = {
  // debug: process.env.NODE_ENV === 'development',
  client: 'mysql',
  connection: connection,
  pool: {
    min: 1,
    max: 10,
  },
  migrations: {
    tableName: '_migrations',
    directory: 'database/migrations',
  },
  seeds: {
    directory: 'database/seeds',
  },
};

module.exports = {
  development: config,
  staging: config,
  production: config,
};
